package com.ext.portlet.myprofile.action;
import java.util.Date;
import java.util.List;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletConfig;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.ext.portlet.myprofile.model.MyProfile;
import com.ext.portlet.myprofile.model.impl.MyProfileImpl;
import com.ext.portlet.myprofile.service.MyProfileLocalServiceUtil;
import com.ext.portlet.myprofile.service.persistence.MyProfileUtil;
import com.ext.portlet.registration.model.CollOthers;
import com.ext.portlet.registration.model.CollStudent;
import com.ext.portlet.registration.model.College;
import com.ext.portlet.registration.model.Mainstay;
import com.ext.portlet.registration.model.Others;
import com.ext.portlet.registration.model.Parent;
import com.ext.portlet.registration.model.School;
import com.ext.portlet.registration.model.Staff;
import com.ext.portlet.registration.model.Student;
import com.ext.portlet.registration.model.Teacher;
import com.ext.portlet.registration.service.CollOthersLocalServiceUtil;
import com.ext.portlet.registration.service.CollStudentLocalServiceUtil;
import com.ext.portlet.registration.service.CollegeLocalServiceUtil;
import com.ext.portlet.registration.service.MainstayLocalServiceUtil;
import com.ext.portlet.registration.service.OthersLocalServiceUtil;
import com.ext.portlet.registration.service.ParentLocalServiceUtil;
import com.ext.portlet.registration.service.SchoolLocalServiceUtil;
import com.ext.portlet.registration.service.StaffLocalServiceUtil;
import com.ext.portlet.registration.service.StudentLocalServiceUtil;
import com.ext.portlet.registration.service.TeacherLocalServiceUtil;
import com.ext.portlet.registration.service.persistence.CollOthersUtil;
import com.ext.portlet.registration.service.persistence.CollStudentUtil;
import com.ext.portlet.registration.service.persistence.CollegeUtil;
import com.ext.portlet.registration.service.persistence.MainstayUtil;
import com.ext.portlet.registration.service.persistence.OthersUtil;
import com.ext.portlet.registration.service.persistence.ParentUtil;
import com.ext.portlet.registration.service.persistence.SchoolUtil;
import com.ext.portlet.registration.service.persistence.StaffUtil;
import com.ext.portlet.registration.service.persistence.StudentUtil;
import com.ext.portlet.registration.service.persistence.TeacherUtil;
import com.liferay.counter.service.CounterServiceUtil;
import com.liferay.portal.ContactBirthdayException;
import com.liferay.portal.kernel.util.Constants;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.model.Address;
import com.liferay.portal.model.Contact;
import com.liferay.portal.model.Phone;
import com.liferay.portal.model.User;
import com.liferay.portal.service.AddressLocalServiceUtil;
import com.liferay.portal.service.ContactLocalServiceUtil;
import com.liferay.portal.service.PhoneLocalServiceUtil;
import com.liferay.portal.service.UserLocalServiceUtil;
import com.liferay.portal.struts.PortletAction;
import com.liferay.portal.theme.ThemeDisplay;
import com.liferay.portal.util.PortalUtil;


public class MyProfileAction extends PortletAction{
	
	public void processAction(ActionMapping mapping, ActionForm form,
			PortletConfig config, ActionRequest req, ActionResponse res)
			throws Exception {

		String cmd = ParamUtil.getString(req, Constants.CMD);			

	    ThemeDisplay theme = (ThemeDisplay)req.getAttribute(WebKeys.THEME_DISPLAY);
	    long companyId = theme.getCompanyId();	    
		long userId = theme.getUserId();	
		
			if (cmd.equals("PREVIEW")) {				
				setForward(req, "portlet.ext.myprofile.details");
			}
			
			if (cmd.equals("APPROVE")) {
				setForward(req, "portlet.ext.myprofile.add");
			}
		
			if (cmd.equals("ADD")) {
				
				String experience = req.getParameter("experience");
				String family = req.getParameter("family");				
				String strength = req.getParameter("strength");
				String interests = req.getParameter("interests");
				String goals = req.getParameter("goals");
				String employment = req.getParameter("employment");
				String hobbies = req.getParameter("hobbies");
				String education = req.getParameter("education");
				String aboutMe = req.getParameter("aboutMe");				
				String myFavoirate = req.getParameter("myFavoirate");
				String languages = req.getParameter("languages");
				String weakness = req.getParameter("weakness");
								
				MyProfile myprofile = new MyProfileImpl();
				myprofile.setPrimaryKey(CounterServiceUtil.increment(MyProfile.class.getName()));
				myprofile.setNew(true);
				myprofile.setUserId(userId);
				myprofile.setCompanyId(companyId);
				myprofile.setExperience(experience);
				myprofile.setFamily(family);			
				myprofile.setStrength(strength);
				myprofile.setInterests(interests);
				myprofile.setGoals(goals);
				myprofile.setEmployment(employment);
				myprofile.setHobbies(hobbies);
				myprofile.setEducation(education);
				myprofile.setAboutMe(aboutMe);				
				myprofile.setMyFavoirate(myFavoirate);
				myprofile.setLanguages(languages);
				myprofile.setWeakness(weakness);
				myprofile.setCreateDate(new Date());
				MyProfileLocalServiceUtil.getService().addMyProfile(myprofile);							
				setForward(req, "portlet.ext.myprofile.details");				

			} else if (cmd.equals("EDIT")) {
				long profileId = ParamUtil.getLong(req, "profileId");
				req.setAttribute("profileId", profileId);				
				setForward(req, "portlet.ext.myprofile.edit");
				
			} else if (cmd.equals("UPDATE")) {
				
				long profileId = ParamUtil.getLong(req, "profileId");
				long addressId = ParamUtil.getLong(req, "addressId");	
				long phoneId = ParamUtil.getLong(req, "phoneId");
					
				String experience = req.getParameter("experience");
				String family = req.getParameter("family");				
				String strength = req.getParameter("strength");
				String interests = req.getParameter("interests");
				String goals = req.getParameter("goals");
				String employment = req.getParameter("employment");
				String hobbies = req.getParameter("hobbies");
				String education = req.getParameter("education");
				String aboutMe = req.getParameter("aboutMe");				
				String myFavoirate = req.getParameter("myFavoirate");
				String languages = req.getParameter("languages");
				String weakness = req.getParameter("weakness");
				
				int prefixId = ParamUtil.getInteger(req,"prefixId");
				String firstName = req.getParameter("firstName");
				String lastName = req.getParameter("lastName");
				String emailAddress = req.getParameter("emailAddress");
				String jobTitle = req.getParameter("jobTitle");
				int birthdayMonth = ParamUtil.getInteger(req, "birthdayMonth");
				int birthdayDay = ParamUtil.getInteger(req, "birthdayDay");
				int birthdayYear = ParamUtil.getInteger(req, "birthdayYear");
				Date birthDay = PortalUtil.getDate(
						birthdayMonth, birthdayDay, birthdayYear,
						new ContactBirthdayException());
				
				MyProfile myprofile = MyProfileUtil.findByPrimaryKey(profileId);	
				
				myprofile.setProfileId(profileId);
				myprofile.setUserId(userId);
				myprofile.setCompanyId(companyId);
				myprofile.setExperience(experience);
				myprofile.setFamily(family);				
				myprofile.setStrength(strength);
				myprofile.setInterests(interests);
				myprofile.setGoals(goals);
				myprofile.setEmployment(employment);
				myprofile.setHobbies(hobbies);
				myprofile.setEducation(education);
				myprofile.setAboutMe(aboutMe);				
				myprofile.setMyFavoirate(myFavoirate);
				myprofile.setLanguages(languages);
				myprofile.setWeakness(weakness);
				myprofile.setModifiedDate(new Date());
				MyProfileLocalServiceUtil.getService().updateMyProfile(myprofile);	
				
				User use = UserLocalServiceUtil.getUser(userId);
				
				use.setEmailAddress(emailAddress);
				UserLocalServiceUtil.getService().updateUser(use);
				
				Contact ct = ContactLocalServiceUtil.getContact(use.getContactId());
				ct.setPrefixId(prefixId);
				ct.setFirstName(firstName);
				ct.setLastName(lastName);
				ct.setJobTitle(jobTitle);				
				ct.setBirthday(birthDay);				
				ContactLocalServiceUtil.getService().updateContact(ct);				
				
				String zip = req.getParameter("zip");
				String street = req.getParameter("street1");
				Address addr = AddressLocalServiceUtil.getAddress(addressId);
				addr.setZip(zip);
				addr.setStreet1(street);
				AddressLocalServiceUtil.getService().updateAddress(addr);	
				
				String phone =req.getParameter("number");	
				Phone ph = PhoneLocalServiceUtil.getPhone(phoneId);
				ph.setNumber(phone);
				PhoneLocalServiceUtil.getService().updatePhone(ph);
				
				if(use.getComments().equals("school")){
					School sc = SchoolUtil.fetchByMyProfileUserId(userId);	
					String website = req.getParameter("website");
					int noOfStudents = ParamUtil.getInteger(req,"noOfStudents");
					int noOfTeachers =ParamUtil.getInteger(req,"noOfTeachers");
					
					int dateOfEstablishmentMonth = ParamUtil.getInteger(req, "dateOfEstablishmentMonth");
					int dateOfEstablishmentDay = ParamUtil.getInteger(req, "dateOfEstablishmentDay");
					int dateOfEstablishmentYear = ParamUtil.getInteger(req, "dateOfEstablishmentYear");
					Date dateOfEstablishment = PortalUtil.getDate(
							dateOfEstablishmentMonth, dateOfEstablishmentDay, dateOfEstablishmentYear,
							new ContactBirthdayException());
					
					sc.setCpFirstName(firstName);
					sc.setCpLastName(lastName);
					sc.setJobTitle(jobTitle);
					sc.setStreet1(street);
					sc.setPinCode(zip);
					sc.setContactNo(phone);
					sc.setWebsite(website);
					sc.setNoOfStudents(noOfStudents);
					sc.setNoOfTeachers(noOfTeachers);
					sc.setDateOfEstablishment(dateOfEstablishment);
					SchoolLocalServiceUtil.getService().updateSchool(sc);				
				}
				if(use.getComments().equals("teacher")){
					Teacher tr = TeacherUtil.fetchByMyProfileUserId(userId);
					String qualification = req.getParameter("qualification");					
					tr.setFirstName(firstName);
					tr.setLastName(lastName);
					tr.setJobTitle(jobTitle);
					tr.setStreet1(street);
					tr.setPinCode(zip);
					tr.setContactNo(phone);
					tr.setQualification(qualification);
					TeacherLocalServiceUtil.getService().updateTeacher(tr);				
				}
				if(use.getComments().equals("student")){
					Student st = StudentUtil.fetchByMyProfileUserId(userId);					
					st.setFirstName(firstName);
					st.setLastName(lastName);
					st.setStandard(jobTitle);
					st.setStreet1(street);
					st.setPinCode(zip);
					st.setContactNo(phone);
					StudentLocalServiceUtil.getService().updateStudent(st);				
				}
				if(use.getComments().equals("parent")){
					Parent pt = ParentUtil.fetchByMyProfileUserId(userId);					
					pt.setFirstName(firstName);
					pt.setLastName(lastName);
					pt.setJobTitle(jobTitle);
					pt.setStreet1(street);
					pt.setPinCode(zip);
					pt.setContactNo(phone);
					ParentLocalServiceUtil.getService().updateParent(pt);				
				}
				
				if(use.getComments().equals("others")){
					Others os = OthersUtil.fetchByMyProfileUserId(userId);					
					os.setFirstName(firstName);
					os.setLastName(lastName);
					os.setJobTitle(jobTitle);
					os.setStreet1(street);
					os.setPinCode(zip);
					os.setContactNo(phone);
					OthersLocalServiceUtil.getService().updateOthers(os);				
				}
				if(use.getComments().equals("staff")){
					Staff sf = StaffUtil.fetchByMyProfileUserId(userId);	
					String qualification = req.getParameter("qualification");
					sf.setFirstName(firstName);
					sf.setLastName(lastName);
					sf.setJobTitle(jobTitle);
					sf.setStreet1(street);
					sf.setPinCode(zip);
					sf.setContactNo(phone);
					sf.setQualification(qualification);
					StaffLocalServiceUtil.getService().updateStaff(sf);				
				}
				if(use.getComments().equals("collOthers")){
					CollOthers co = CollOthersUtil.fetchByMyProfileUserId(userId);					
					co.setFirstName(firstName);
					co.setLastName(lastName);
					co.setJobTitle(jobTitle);
					co.setStreet1(street);
					co.setPinCode(zip);
					co.setContactNo(phone);
					CollOthersLocalServiceUtil.getService().updateCollOthers(co);				
				}
				if(use.getComments().equals("collStudent")){
					CollStudent cs = CollStudentUtil.fetchByMyProfileUserId(userId);
					String others = req.getParameter("others");					
					
					cs.setFirstName(firstName);
					cs.setLastName(lastName);
					if(jobTitle == null){
						cs.setOthers(others);
					}
					cs.setDegree(jobTitle);
					cs.setStreet1(street);
					cs.setPinCode(zip);
					cs.setContactNo(phone);
					CollStudentLocalServiceUtil.getService().updateCollStudent(cs);				
				}
				if(use.getComments().equals("college")){
					College cg = CollegeUtil.fetchByMyProfileUserId(userId);
					String website = req.getParameter("website");
					int noOfStudents = ParamUtil.getInteger(req,"noOfStudents");
					int noOfStaffs =ParamUtil.getInteger(req,"noOfStaffs");
					
					int dateOfEstablishmentMonth = ParamUtil.getInteger(req, "dateOfEstablishmentMonth");
					int dateOfEstablishmentDay = ParamUtil.getInteger(req, "dateOfEstablishmentDay");
					int dateOfEstablishmentYear = ParamUtil.getInteger(req, "dateOfEstablishmentYear");
					Date dateOfEstablishment = PortalUtil.getDate(
							dateOfEstablishmentMonth, dateOfEstablishmentDay, dateOfEstablishmentYear,
							new ContactBirthdayException());
					
					cg.setCpFirstName(firstName);
					cg.setCpLastName(lastName);
					cg.setJobTitle(jobTitle);
					cg.setStreet1(street);
					cg.setPinCode(zip);
					cg.setContactNo(phone);
					cg.setWebsite(website);
					cg.setNoOfStudents(noOfStudents);
					cg.setNoOfStaffs(noOfStaffs);
					cg.setDateOfEstablishment(dateOfEstablishment);
					
					CollegeLocalServiceUtil.getService().updateCollege(cg);				
				}
				if(use.getComments().equals("mainstay")){
					Mainstay my = MainstayUtil.fetchByMyProfileUserId(userId);					
					my.setFirstName(firstName);
					my.setLastName(lastName);
					my.setJobTitle(jobTitle);
					my.setStreet1(street);
					my.setPinCode(zip);
					my.setContactNo(phone);
					MainstayLocalServiceUtil.getService().updateMainstay(my);				
				}
				setForward(req, "portlet.ext.myprofile.details");
				
			}
	}

	@Override
	public ActionForward render(ActionMapping mapping, ActionForm form,
			PortletConfig config, RenderRequest req, RenderResponse res)
			throws Exception {
		if (getForward(req) != null && !getForward(req).equals("")) {
			return mapping.findForward(getForward(req));
		} else {

			return mapping.findForward("portlet.ext.myprofile.view");
		}
	}

	
}
